package Eyjafjalla.action;

import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.GameActionManager;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.Iterator;

public class ChooseHandToCloudAction extends AbstractGameAction {
    private static final UIStrings uiStrings;
    public static final String[] TEXT;
    private AbstractPlayer p;
    private boolean isRandom;
    private boolean endTurn;
    private boolean canPickZero;
    public static int numClouded;
    private static final float DURATION;

    public ChooseHandToCloudAction(int amount, boolean isRandom) {
        this(amount, isRandom,false ,false);
    }

    public ChooseHandToCloudAction(int amount, boolean isRandom,boolean canPickZero, boolean endTurn) {
        this.p = AbstractDungeon.player;
        this.isRandom = isRandom;
        this.canPickZero = canPickZero;
        this.setValues(null, null, amount);
        this.actionType = ActionType.CARD_MANIPULATION;
        this.endTurn = endTurn;
        this.duration = DURATION;
    }


    public void update() {
        if (this.duration == DURATION) {
            if (AbstractDungeon.getMonsters().areMonstersBasicallyDead()) {
                this.isDone = true;
                return;
            }

            int size;
            if (!canPickZero&&this.p.hand.size() <= this.amount&&(this.p.hand.size()+CloudPatch.cloudGroup.size())<=5) {
                this.amount = this.p.hand.size();
                size = this.p.hand.size();

                for(int i = 0; i < size; ++i) {
                    AbstractCard c = this.p.hand.getTopCard();
                    CloudPatch.moveToCloud(this.p.hand,c);
                }

                AbstractDungeon.player.hand.applyPowers();
                this.tickDuration();
                return;
            }

            if (!this.isRandom) {
                if (this.amount < 0) {
                    AbstractDungeon.handCardSelectScreen.open(TEXT[0], 99, true, true);
                    AbstractDungeon.player.hand.applyPowers();
                    this.tickDuration();
                    return;
                }
                if(this.amount > 5-CloudPatch.cloudGroup.size())
                    this.amount = 5-CloudPatch.cloudGroup.size();
                if(this.amount > p.hand.size())
                    this.amount = p.hand.size();
                numClouded = this.amount;
                if (this.amount>0) {
                    AbstractDungeon.handCardSelectScreen.open(TEXT[0], this.amount, canPickZero,canPickZero);
                }
                else {
                    CloudPatch.createCloudFullDialog();
                    this.isDone = true;
                    return;
                }

                AbstractDungeon.player.hand.applyPowers();
                this.tickDuration();
                return;
            }

            for(int i = 0; i < this.amount&&CloudPatch.cloudGroup.size()<5; ++i) {
                AbstractCard c = this.p.hand.getRandomCard(AbstractDungeon.cardRandomRng);
                CloudPatch.moveToCloud(this.p.hand,c);
            }
        }

        if (!AbstractDungeon.handCardSelectScreen.wereCardsRetrieved) {
            Iterator var4 = AbstractDungeon.handCardSelectScreen.selectedCards.group.iterator();

            while(var4.hasNext()) {
                AbstractCard c = (AbstractCard)var4.next();
                if(CloudPatch.cloudGroup.size()<5)
                    CloudPatch.moveToCloud(this.p.hand,c);
            }

            AbstractDungeon.handCardSelectScreen.wereCardsRetrieved = true;
        }

        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
        TEXT = uiStrings.TEXT;
        DURATION = Settings.ACTION_DUR_XFAST;
    }
}

